package com.hele.sts.dao;

import com.hele.sts.pub.ConnectDB;
import com.hele.sts.pub.SpringContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class DaArchTidDao {
    static Logger logger = LoggerFactory.getLogger(DaArchTidDao.class);
    static String TABLE = "da_arch_tid";

    public static DaArchTid queryDaxxByNbxh(String nbxh)
    {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = null;
        ConnectDB connsyb = null;

        sql = "select * from "+ TABLE +" where nbxh=?";

        DaArchTid daArchTid = new DaArchTid();
        daArchTid.setNbxh(nbxh);
        logger.debug(sql);
        try
        {
            connsyb = SpringContextHolder.getBean(ConnectDB.class);
            conn = connsyb.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,nbxh);
            rs = pstmt.executeQuery();
            //List list = new ArrayList();
            String cjxx = "";
            String tids = "";
            String cjh = "";
            int i = 0;
            HashSet<String> cjhSet = new HashSet<>();
            while (rs.next()) {
                i++;
                String tid = rs.getString("archtid");
                if(tid!=null){
                    tids+=tid+",";
                }

                String epc = rs.getString("pripid");
                if(epc!=null){
                    cjhSet.add(epc);
                    //cjxx += convertCjbToMsg(epc)+",";
                    // += epc +",";
                }
            }
            if(tids.indexOf(",")>0){
               tids = tids.substring(0,tids.length()-1);
            }
            Iterator<String> iter = cjhSet.iterator();
            while(iter.hasNext()){
                String epcc = iter.next();
                cjh  += epcc +",\n";
                cjxx += convertCjbToMsg(epcc)+",\n";
            }
            if(cjh.indexOf(",")>0){
                cjh = cjh.substring(0,cjh.length()-2);
                cjxx = cjxx.substring(0,cjxx.length()-2);
            }

            daArchTid.setArchtid(tids);
            daArchTid.setPripid(cjh);
            daArchTid.setCjxx(cjxx);
        }
        catch (SQLException e)
        {
            logger.info("db error", e);
        }
        finally
        {
            DbClose.freePreparedStatement(pstmt);
            DbClose.freeConnection(conn);
            DbClose.freeResultSet(rs);
        }


        return daArchTid;
    }

    private static DaArchTid rsToBean(ResultSet rs) throws SQLException {
        return null;
    }

    public static String convertCjbToMsg(String cjbEpc) {
        //A00000000001020700200504
        if (StringUtils.isEmpty(cjbEpc) || cjbEpc.length() != 24) {
            return cjbEpc;
        }
        if (!cjbEpc.startsWith("A0000000000102")) {
            return cjbEpc;
        }
        StringBuilder sb = new StringBuilder();
        String jh = cjbEpc.substring(14, 16);
        String hh = cjbEpc.substring(16, 19);
        String lh = cjbEpc.substring(19, 22);
        String ch = cjbEpc.substring(22, 24);

        sb.append(jh).append("架 ").append(hh).append("行 ").append(lh).append("列 ").append(ch).append("层");
        return sb.toString();
    }
}
